﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//引入命名空间
using System.Data;
public partial class UserMan_gxgrzl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
            BindDay();
            BindProvince();
            ReadInfo();
        }
    }
    protected void drpProblem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpProblem.SelectedIndex == 4)
        {
            txtProblem.Visible = true;
        }
        else
        {
            txtProblem.Visible = false;
        }
    }
    protected void BindYear()
    {
        drpYear.Items.Clear();
        int startYear = DateTime.Now.Year - 10;
        int currentYear = DateTime.Now.Year;
        for (int i = startYear; i <= currentYear; i++)
        {
            drpYear.Items.Add(new ListItem(i.ToString()));
        }
        drpYear.SelectedValue = currentYear.ToString();
    }
    protected void BindMonth()
    {
        drpMonth.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            drpMonth.Items.Add(i.ToString());
        }
    }
    protected void BindDay()
    {
        drpDay.Items.Clear();
        string year = drpYear.SelectedValue;
        string month = drpMonth.SelectedValue;
        int days = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
        for (int i = 1; i <= days; i++)
        {
            drpDay.Items.Add(i.ToString());
        }
    }
    protected void BindProvince()
    {

        users tmp = new users();
        drpProvince.DataSource = tmp.BindPro().Tables[0].DefaultView;
        drpProvince.DataTextField = "FName";
        drpProvince.DataValueField = "FID";
        drpProvince.DataBind();


    }
    protected void BindCity(string proid)
    {
        users tmp = new users();
        drpCity.DataSource = tmp.BindCity(proid).Tables[0].DefaultView;
        drpCity.DataValueField = "FCityID";
        drpCity.DataTextField = "FCityName";
        drpCity.DataBind();
    }


    protected void ReadInfo()//把用户的个人信息读取出来
    {
        string id = "";
        if (Request.QueryString["u_ID"] != null)
        {
            id = Request.QueryString["u_ID"].ToString();
        }
        txtID.Text = id;

        users tmp = new users();
        tmp.id = id;


        SqlDataReader rd = tmp.Getoneuser(tmp);//读取器
        while (rd.Read())
        {
            txtPS.Text = rd["u_PASSWORD"].ToString().Trim();
            txtName.Text = rd["u_Name"].ToString().Trim();
            string birth = rd["u_BIRTHDAY"].ToString().Trim();
            if (birth.Length != 0)
            {
                string year = birth.Substring(0, 4);
                int month = Convert.ToInt32(birth.Substring(4, 2));
                int day = Convert.ToInt32(birth.Substring(6, 2));
                drpYear.SelectedValue = year;
                drpMonth.SelectedValue = month.ToString();
                drpDay.SelectedValue = day.ToString();
            }

            string proid = rd["u_PROVINCE"].ToString().Trim();
            if (proid.Length != 0)
            {
                drpProvince.SelectedValue = proid;
                BindCity(drpProvince.SelectedValue);
                string cityid = rd["u_CITY"].ToString().Trim();
                if (cityid.Length != 0)
                {
                    drpCity.SelectedValue = cityid;
                }
            }
            txtQQ.Text = rd["u_QQ"].ToString().Trim();
            txtMail.Text = rd["u_MAIL"].ToString().Trim();
            txtPhone.Text = rd["u_PHONE"].ToString().Trim();
            txtAddress.Text = rd["u_ADDRESS"].ToString().Trim();

            string problem = rd["u_PROBLEM"].ToString().Trim();
            if (problem.Length != 0)
            {
                drpCity.SelectedValue = problem;
            }
            txtAnswer.Text = rd["u_ANSWER"].ToString().Trim();
        }
        rd.Close();
    }


    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }
    protected void drpMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDay();
    }
    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCity(drpProvince.SelectedValue.Trim());
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string id = txtID.Text.Trim();
        string name = txtName.Text.Trim();
        string ps = txtPS.Text.Trim();


        string year = drpYear.SelectedValue.Trim();
        string month = drpMonth.SelectedValue.Trim();
        string day = drpDay.SelectedValue.Trim();

        string birth = "";
        if (month.Length == 1)
        {
            month = "0" + month;

        }
        if (day.Length == 1)
        {
            day = "0" + day;

        }
        birth = year + month + day;

        string proid = drpProvince.SelectedValue.Trim();
        string cityid = drpCity.SelectedValue.Trim();


        string qq = txtQQ.Text.Trim();
        string mail = txtMail.Text.Trim();
        string phone = txtPhone.Text.Trim();
        string address = txtAddress.Text.Trim();
        string problem = drpProblem.SelectedValue.Trim();
        string answer = txtAnswer.Text.Trim();

        users tmp = new users();
        tmp.id = id;
        tmp.name = name;
        tmp.ps = ps;
        tmp.birth = birth;
        tmp.province = proid;
        tmp.city = cityid;
        tmp.qq = qq;
        tmp.mail = mail;
        tmp.phone = phone;
        tmp.address = address;
        tmp.problem = problem;
        tmp.answer = answer;
        int result = tmp.Updateoneuser(tmp);
        if (result > 0)
        {
            Response.Write("<script>alert(\"更新个人资料成功！\")</script>");
        }
        else
        {
            Response.Write("<script>alert(\"对不起个人资料更新失败！\")</script>");
        }
    }
}